<template>
	<view>
		<view class="fixbox" :style="{ height: fixboxHeight + 'px' }">
		  <view class="fix">
		    <top-status></top-status>
		    <view class="tabs">
		      消息
		    </view>
		  </view>
		</view>
		<view @click="nav_to('/pages/other/notice')" class="list-item">
			<view class="item-icon">
				<image src="https://golfdate.oss-cn-shenzhen.aliyuncs.com/upload/17109999487431091.png"> </image>
			</view>
			<view class="item-body">
				<view class="item-name"> 系统通知 </view>
				<!-- <view class="list-msg u-line-1">{{ systemCount == 0 ? '' : '您有' + systemCount + '条消息' }}</view> -->
				<view class="item-count" v-if="systemCount>0">{{systemCount > 99? '99+':systemCount}}</view>
			</view>
		</view>
		<!-- <view @click="nav_to('/pages/friend/new_friend_list')" class="list-item">
			<view class="item-icon">
				<image src="https://golfdate.oss-cn-shenzhen.aliyuncs.com/upload/1710383495763867.png"> </image>
			</view>
			<view class="item-body">
				<view class="item-name"> 新的朋友 </view>
				<view class="item-count"><uni-badge :text="friendCount" size="normal"></uni-badge></view>
			</view>
		</view> -->
		<uni-swipe-action ref="swipeAction">
			<uni-swipe-action-item v-for="(l, i) in list" :right-options="l.options" :key="i" @click="swipeClick($event, i)">
				<view class="list-cell" @click="route(l)">
					<view class="list-icon">
						<image :src="l.conversationAvatarUrl" mode="aspectFill"> </image>
						<view class="list-count" v-if="l.unreadMessageCount > 0">
							<!-- <uni-badge :text="l.unreadMessageCount"></uni-badge> -->
							{{l.unreadMessageCount}}
						</view>
					</view>
					<view class="list-body">
						<view class="list-name"> {{l.conversationName}} </view>
						<block v-if="l.lastMessage">
							<view v-if="l.lastMessage.type===1" class="list-msg"> {{l.lastMessage.message}} </view>
							<view v-else-if="l.lastMessage.type===11" class="list-msg"> [图片] </view>
							<view v-else-if="l.lastMessage.type===13" class="list-msg"> [语音] </view>
							<view v-else-if="l.lastMessage.type===200" class="list-msg">
								<!-- 自定义文本 -->
								{{subTypeText(l.lastMessage.subType)}}
							</view>
						</block>
					</view>
					<view v-if="l.lastMessage" class="list-time">{{timeformat(l.lastMessage.timestamp)}}</view>
				</view>
			</uni-swipe-action-item>
		</uni-swipe-action>
	</view>
</template>

<script>
	import { dateformat } from '@/im/js/utils'
	import topStatus from "@/components/topStatus/topStatus.vue";
	export default {
		components: {
			topStatus
		},
		data() {
			return {
				friendCount: 0,
				systemCount: 0,
				conversationCount: 0,
				list: [],
				fixboxHeight:88
			}
		},
		async onLoad() {
			console.log('3333333');
			uni.$on('conversationChanged', res => {
				console.log('2222');
				res.forEach(v => {
					const l = v.conversation
					const index = this.list.findIndex(x => x.conversationID === l.conversationID);
					if (index !== -1) {
						this.conversationCount -= this.list[index].unreadMessageCount;
						this.list.splice(index, 1);
					}
					this.conversationCount += l.unreadMessageCount;
					this.list.unshift(l);
					console.log('11111');
					console.log(this.list,'list');
					this.calcNum()
				})
			})
		},
		onShow() {
			this.queryConversationList()
			this.getFriend()
			this.getSystem()
			this.getHeight()
			this.$store.dispatch('getMyNum')
		},
		methods: {
			getHeight() {
			  let that = this;
			  setTimeout(() => {
			    const query = uni.createSelectorQuery().in(that);
			    query.select('.fix').boundingClientRect(data => {
			      if (data) {
			        // data.height 就是元素的高度
			        that.fixboxHeight = data.height;
			      }
			    }).exec();
			  }, 500);
			
			},
			async getFriend() {
				let res = await this.$api.post('/Friend/PendingApply');
				this.friendCount = res.data.length;
				this.calcNum()
			},
			async getSystem() {
				let res = await this.$api.post('/MessageNotify/MyNotReadMessage');
				this.systemCount = res.data.length;
				this.calcNum()
			},
			async queryConversationList() {
				const res = await this.$im.queryConversationList()
				if (res.code === 0) {
					this.conversationCount = 0
					this.list = res.data.map(v => {
						this.conversationCount += v.unreadMessageCount;
						v.options = [{
							text: '删除',
							style: {
								backgroundColor: '#F56C6C'
							}
						}]
						return v
					})
					console.log(this.list,'listlist');
					this.calcNum()
				}
			},
			calcNum() {
				const num = this.conversationCount + this.friendCount + this.systemCount
				this.$store.commit('setTabNum', num);
			},
			async swipeClick(e, i) {
				const l = this.list[i]
				const res = await this.$im.deleteConversation({ conversationID: l.conversationID, conversationType: l.type })
				if (res.code === 0) {
					this.list.splice(i, 1)
					return
				}
				uni.showToast({
					icon: 'none',
					title: '删除失败'
				})
			},
			async route(l) {
				this.$im.clearConversationCount({ conversationID: l.conversationID, conversationType: l.type })
				const page = l.type === 0 ? 'chat' : 'chat_group' //单聊：群聊
				uni.navigateTo({
					url: `/pages/im/${page}?conversationID=${l.conversationID}&conversationName=${l.conversationName}`
				})
			}
		},
		computed: {
			timeformat() {
				return timestamp => {
					return dateformat(timestamp)
				}
			},
			subTypeText() {
				return (subType, desc) => {
					const classText = {
						10: '[礼物赠送]',
						11: '[系统消息：礼物赠送]',
						12: '[微信申请]',
						13: '[系统消息：微信申请]',
						14: '[微信号]',
						15: '[系统消息：会员赠送]',
						30: '[添加好友提示]',
						31: '[系统消息]',
						50: '[系统消息]',
						51: '[加入群聊]',
						52: '[加入群聊]',
						53: '[退出群聊]',
						54: '[退出群聊]',
						60: '[系统消息]',
					}
					return classText[subType] || ''
				}
			}
		}
	}
</script>

<style lang="scss">
	page {
		background-color: #fdf4ff;
	}
	.fixbox {
		
	  .fix {
	    position: sticky;
	    background-image: linear-gradient(180deg, #c9ceff 0%, #fdf4ff 100%);
	    width: 100%;
	    top: 0;
	    left: 0;
	    z-index: 999;
	  }
	  
	  .tabs {
	    box-sizing: border-box;
	    z-index: 200;
	    height: 44px;
	    width: 750rpx;
	    display: flex;
	    align-items: center;
	    justify-content: center;
	    font-size: 32rpx;
	    font-weight: bold;
	  }
	}
	view {
		font-size: 28rpx;
		line-height: 1.6;
		box-sizing: border-box;
	}

	.list-item {
		display: flex;
		padding: 20rpx 35rpx;
		position: relative;

		// &::after {
		// 	position: absolute;
		// 	bottom: 0;
		// 	right: 0rpx;
		// 	left: 150rpx;
		// 	height: 2rpx;
		// 	content: '';
		// 	transform: scaleY(.6);
		// 	background-color: #404040;
		// }

		.item-icon {
			width: 100rpx;
			height: 100rpx;
			display: flex;
			align-items: center;
			justify-content: center;

			image {
				width: 88rpx;
				height: 88rpx;
				display: block;
			}
		}

		.item-body {
			flex: 1;
			padding-left: 30rpx;
			display: flex;
			align-items: center;
			justify-content: space-between;

			.item-name {
				margin-top: 5rpx;
				font-size: 32rpx;
				color: #333333;
				font-weight: bold;
			}

			.item-count {
				background-color: #EF4034;
				width: 36rpx;
				height: 36rpx;
				text-align: center;
				line-height: 36rpx;
				border-radius: 50%;
				color: #fff;
				font-size: 24rpx;
			}
		}
	}

	.list-cell {
		display: flex;
		padding: 20rpx 35rpx;
		position: relative;

		// &::after {
		// 	position: absolute;
		// 	bottom: 0;
		// 	right: 0rpx;
		// 	left: 150rpx;
		// 	height: 2rpx;
		// 	content: '';
		// 	transform: scaleY(.6);
		// 	background-color: #404040;
		// }

		.list-icon {
			width: 100rpx;
			height: 100rpx;
			position: relative;

			image {
				width: 100%;
				height: 100%;
				border-radius: 10rpx;
				display: block;
			}

			.list-count {
				position: absolute;
				right: -10rpx;
				top: -8rpx;
				background-color: #EF4034;
				width: 36rpx;
				height: 36rpx;
				text-align: center;
				border-radius: 50%;
				color: #fff;
				font-size: 24rpx;
			}
		}

		.list-body {
			flex: 1;
			min-width: 0;
			padding-left: 30rpx;

			.list-name {
				font-size: 32rpx;
				font-weight: 600;
				color: #333333;
				overflow: hidden;
				text-overflow: ellipsis;
				white-space: nowrap;
			}

			.list-msg {
				margin-top: 5rpx;
				color: #666666;
				font-size: 28rpx;
				overflow: hidden;
				text-overflow: ellipsis;
				white-space: nowrap;
			}
		}

		.list-time {
			color: #999999;
			margin-left: 40rpx;
			padding-top: 10rpx;
			width: 242rpx;
			font-size: 24rpx;
			line-height: 36rpx;
			text-align: right;
		}
	}
</style>